<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="团类目编号" v-model="listQuery.groupsId"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
          <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

          <el-table-column align="center" min-width="120px" label="平台编号" prop="platformId">
          </el-table-column>
          <el-table-column align="center" min-width="120px" label="商户账号" prop="merId">
          </el-table-column>
          <el-table-column align="center" min-width="120px" label="团类目编号" prop="groupsId">
          </el-table-column>       
          <el-table-column align="center" min-width="120px" label="商品编号" prop="goodsId">
          </el-table-column>       
          <el-table-column align="center" min-width="120px" label="团名称" prop="groupName">
          </el-table-column>   
          <el-table-column align="center" min-width="100px" label="金额" prop="txAmt">
          </el-table-column>   
          <el-table-column align="center" min-width="100px" label="限制人数" prop="peopleNum">
          </el-table-column>   
          <el-table-column align="center" min-width="200px" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="primary" size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 修改对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="addDialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="平台编号" prop="platformId">
              <el-input v-model="dataForm.platformId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="商品编号" prop="goodsId">
              <el-select v-model="dataForm.goodsId" placeholder="请选择" size="small"  clearable="">
                <el-option v-for="(g,index) in goodss" :label="g.goodsName" :value="g.goodsId" :key="index"></el-option>
              </el-select> 
            </el-form-item>  
            <el-form-item label="团名称" prop="groupName"> 
              <el-input v-model="dataForm.groupName"></el-input>
            </el-form-item>                  
            <el-form-item label="金额" prop="txAmt"> 
              <el-input v-model="dataForm.txAmt"></el-input>
            </el-form-item>  
            <el-form-item label="限制人数" prop="peopleNum"> 
              <el-input v-model="dataForm.peopleNum"></el-input>
            </el-form-item>  
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData('dataForm')">保存</el-button>
            <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateData('dataForm')">保存</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { listFightGroup, addFightGroup, deleteFightGroup, updateFightGroup } from '@/api/cms/fightGroup/category'
import { listFightGoodsInfo } from '@/api/cms/fightGroup/goodsInfo'
import { mapGetters } from 'vuex'

export default {
  name: 'auction',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        groupsId: ''
      },
      goodss: [],
      list: [],
      total: 0,
      listLoading: false,
      addDialogFormVisible: false,
      downloadLoading: false,
      dataForm: {
        platformId: '',
        merId: '',
        goodsId: '',
        groupName: '',
        txAmt: 0,
        peopleNum: 0
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogStatus: '',
      rules: {
        goodsId: [
          {
            required: true,
            message: '请选择商品编号',
            trigger: 'blur'
          }
        ],
        groupName: [
          {
            required: true,
            message: '请输入拼团名称',
            trigger: 'blur'
          }
        ],
        txAmt: [
          {
            required: true,
            message: '请输入金额',
            trigger: 'blur'
          },
          {
            pattern: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
            message: '输入格式错误，请重新输入!'
          }
        ],
        peopleNum: [
          {
            required: true,
            message: '请输入限制人数',
            trigger: 'blur'
          },
          {
            pattern: /^\+?[1-9][0-9]*$/,
            message: '输入格式错误，请重新输入!'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'platformId'
    ])
  },
  created() {
    this.getList()
    this.getGoods()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getGoods() {
      var str = {}
      str.platformId = this.platformId
      listFightGoodsInfo(str).then(response => {
        console.log(response)
        this.goodss = response.data.rows
        console.log('商品')
        console.log(this.goodss)
      }).catch(() => {
        this.goodss = []
      })
    },
    getList() {
      this.listLoading = true
      listFightGroup(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleCreate() {
      this.resetForm()
      this.dataForm.platformId = this.platformId
      this.dialogStatus = 'create'
      this.addDialogFormVisible = true
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          deleteFightGroup(row)
            .then(response => {
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
              this.listLoading = false
            })
        })
    },
    handleUpdate(row) {
      this.dataForm = row
      this.dialogStatus = 'update'
      this.addDialogFormVisible = true
    },
    createData(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加拼团类目, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.listLoading = true
            this.addDialogFormVisible = false
            addFightGroup(this.dataForm).then((response) => {
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 5000
                })
                this.getList()
              }
              this.listLoading = false
            })
            this.dataForm.resetTemp1()
          })
        } else {
          return false
        }
      })
    },
    updateData(formName) {
      this.dataForm.peopleNum = this.dataForm.peopleNum.toString()
      this.dataForm.txAmt = this.dataForm.txAmt.toString()
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.addDialogFormVisible = false
          updateFightGroup(this.dataForm).then(response => {
            if (response.success) {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dataForm = {
        goodsId: '',
        groupName: '',
        txAmt: undefined,
        peopleNum: undefined
      }
    }
  }
}
</script>

<style>

</style>


